package Code;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public Calculator() {
		super();
		// 初始化计算器
		init();
		// 设置计算器的背景颜色
		this.setBackground(Color.LIGHT_GRAY);
		this.setTitle("计算器");
		// 在屏幕(500, 300)坐标处显示计算器
		this.setLocation(500, 300);
		// 不许修改计算器的大小
		this.setResizable(false);
		// 使计算器中各组件大小合适
		this.pack();
	}


	public int getSize() {
		return str.length - pos;
	}

	public synchronized void run1() {
		while (getSize() > 0) {
			if (flag != false) {
				try {
					wait();
				} catch (InterruptedException e) {

				}
			}
			if (pos < n) {
				System.out.println(Thread.currentThread().getName()
						+ " finish " + str[pos]);
				pos++;
				flag = true;
				notify();
			}
		}
	}

	public synchronized void run2() {

		while (getSize() > 0) {
			if (flag != true) {
				try {
					wait();
				} catch (InterruptedException e) {

				}
			}
			if (pos < n) {
				System.out.println(Thread.currentThread().getName()
						+ " finish " + str[pos]);
				pos++;
				flag = false;
				notify();
			}
		}
	}

}

class Worker1 implements Runnable {
	Repo repo;

	public Worker1(Repo repo) {
		this.repo = repo;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		repo.run1();

	}
}

class Worker2 implements Runnable {
	Repo repo;

	public Worker2(Repo repo) {
		this.repo = repo;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		repo.run2();
	}
}